/*
* @名称 auto-brand
* @功能 首页品牌选择
* @author Gina
*/
define(function(require,exports,module){
    var Mustache = require("mustache");
    var $ = require("$");
    function AutoBrand(){
        var _data = null;
        $.ajax({
            type: "POST",
            dataType:"json",
            url:lemon.site_url+"api/auto/auto_brands.php",
            async:false,
            success: function(data){
                _data = data;
            }
        });
        this.brandData = _data;
    }
    module.exports = AutoBrand;
    /*
     * @name getBrand
     * @功能 获取品牌列表
     * @param none
     */
    AutoBrand.prototype.getBrand = function(){
        var that = this,
            brands = [],
            brandData = that.brandData;
        if(brandData["len"]<=0){return false;}
        for(var p in brandData){
            if( p=='len') continue;
            brands.push({"id" : p, "name": brandData[p].name});
        }
        return brands;
    }
    /*
     * @name getModel
     * @功能 获取车系列表
     * @param 品牌ID
     */
    AutoBrand.prototype.getModel = function(brandId){
        var that = this,
            models = [],
            brandData = that.brandData;
        if(brandData[brandId]["len"]<=0){return false;}
        for (var p in brandData[brandId]["model"]){
            models.push({"id" : p, "name": brandData[brandId]["model"][p].name});
        }
        return models;
    }
    /*
     * @name makeSelect
     * @功能 初始化选择框
     * @param none
     */
    AutoBrand.prototype.makeSelect = function(){
        var that = this,
            brands = that.getBrand(),
            brandsData = {"brand":brands},
            brandTpl = "{{#brand}}"+
                "<option value='{{id}}'>{{name}}</option>"+
                "{{/brand}}",
            brandHtml = Mustache.to_html(brandTpl,brandsData);
        $("#index-auto-brand").children("option:gt(0)").remove();
        $("#index-auto-brand").append(brandHtml);
    }

    /*
     * @name changeBrand
     * @功能 修改品牌选择项
     * @param 品牌ID
     */
    AutoBrand.prototype.changeBrand = function(brandId){
        var that = this,
            models = that.getModel(brandId),
            modelsData = {"model":models},
            modelTpl = "{{#model}}"+
                "<option value='{{id}}'>{{name}}</option>"+
                "{{/model}}",
            modelHtml = Mustache.to_html(modelTpl,modelsData);
        $("#index-auto-model").children("option:gt(0)").remove();
        $("#index-auto-model").append(modelHtml);
        $("#index-auto-model").removeAttr("disabled");
    }
    AutoBrand.prototype.change = function(){
        var that = this;
        $("#index-auto-brand").change(function(){
            var brandId = $(this).val().substr(1,$(this).val().length-2),
                link = $("#index-auto-style").attr("val");
            if($(this).val()==0){$("#index-auto-model").prop("disabled",'true'); return;}
            that.changeBrand($(this).val());
            $("#index-auto-style").attr("href",link+brandId);
        });
        $("#index-auto-model").change(function(){
            var brandId =parseInt($(this).val()),
                link = $("#index-auto-style").attr("vel");
            $("#index-auto-style").attr("href",link+brandId)
        });
    }
});